package com.antler.smt.portal.service.impl;

import com.antler.smt.constant.DateTimeConstant;
import com.antler.smt.constant.OrderConstant;
import com.antler.smt.entity.MemberEntity;
import com.antler.smt.entity.OrderEntity;
import com.antler.smt.entity.ProductionLogEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.mapper.MemberEntityMapper;
import com.antler.smt.portal.mapper.OrderEntityMapper;
import com.antler.smt.portal.mapper.ProductionLogEntityMapper;
import com.antler.smt.portal.mapper.ProductionLogInfoMapper;
import com.antler.smt.request.admin.OrderControllerReq;
import com.antler.smt.request.portal.ProductionLogReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.portal.ProductionLogService;
import com.antler.smt.utils.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhangyi
 * @date 2018/5/18 10:06
 */
@Service
public class ProductionLogServiceImpl implements ProductionLogService {

    @Autowired
    private ProductionLogEntityMapper productionLogEntityMapper;

    @Autowired
    private ProductionLogInfoMapper productionLogInfoMapper;

    @Autowired
    private MemberEntityMapper memberEntityMapper;

    @Autowired
    private OrderEntityMapper orderEntityMapper;

    /**
     * 写入日志
     *
     * @param req
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = DateTimeConstant.MINUTE_S, rollbackFor = Exception.class)
    public Result writeLog(ProductionLogReq req) {
        Result result = null;

        ProductionLogEntity productionLogEntity = new ProductionLogEntity();
        OrderEntity orderEntity = orderEntityMapper.selectByPrimaryKey(req.getOrderId());
        Integer status = orderEntity.getStatus();

        if(status.equals(OrderConstant.ORDER_REFUND_FAIL) ||
                status.equals(OrderConstant.ORDER_REFUND) ||
                status.equals(OrderConstant.ORDER_REFUND) ||
                status.equals(OrderConstant.ORDER_APPLY_REFUND)) {
            return new Result(ResultCodeEnum.APPEND_DATA_ERROR,"您的订单正在处理退款！");
           }else{
            productionLogEntity.setLogStatus(0);

            productionLogEntity.setOrderId(req.getOrderId());

            productionLogEntity.setLogContent(req.getLogContent());

            productionLogEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));
            productionLogEntity.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));

            int i = productionLogEntityMapper.insertSelective(productionLogEntity);

            result = (0 == i ? new Result(ResultCodeEnum.APPEND_DATA_ERROR) : new Result(ResultCodeEnum.OPERATION_SUCCESS));

        }
        return result;
    }

    /**
     * 获取该订单下的生产进度
     *
     * @param orderId
     * @return
     */
    @Override
    public Result<PageInfo<ProductionLogEntity>> getLogByCurrentOrder(Long orderId,Long memberId) {
        Result result = null;

        Page<ProductionLogEntity> listProductionLog = productionLogInfoMapper.selectAllProductLog(orderId);

        MemberEntity memberEntity =
                memberEntityMapper.selectByPrimaryKey(memberId);

        int length = listProductionLog.size();

        if(memberEntity.getType().equals(2)) {
            // 去除没有审核的日志结果
            for (int i = 0; i < length; i++) {
                if (listProductionLog.get(i).getLogStatus() == 0) {
                    listProductionLog.get(i).setLogContent("您暂无权限查看");
                }
            }
        }
        if (listProductionLog != null) {
            PageInfo<ProductionLogEntity> listPageInfo = new PageInfo(listProductionLog);
            result = new Result(listPageInfo);
        } else {
            result = new Result(ResultCodeEnum.QUERY_DATA_ERROR, "订单生产进度数据为空！");
        }
        return result;
    }
}
